package timeutil

import "time"

const (
	Fa = "2006-01-02 15:04:05"
)

// 时间util类 当前时间 2021-12-15 22:03:58 -> 获取其它相对时刻时间

// GetStartOfHour now->当前小时开始时间 2021-12-15 22:00:00
func GetStartOfHour(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
}

// GetEndOfHour now->当前小时结束时间 2021-12-15 22:59:59
func GetEndOfHour(t time.Time) time.Time {
	return GetStartOfHour(t).Add(time.Hour - time.Nanosecond)
}

// GetStartOfDay now->当天开始时间 2021-12-15 00:00:00
func GetStartOfDay(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}

// GetEndOfDay now->当天结束时间 2021-12-15 23:59:59
func GetEndOfDay(t time.Time) time.Time {
	return GetStartOfDay(t).AddDate(0, 0, 1).Add(-time.Nanosecond)
}

// GetStartOfMonth now->当月开始时间 2021-12-01 00:00:00
func GetStartOfMonth(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
}

// GetEndOfMonth 当月结束时间 2021-12-31 23:59:59
func GetEndOfMonth(t time.Time) time.Time {
	return GetStartOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
}

// GetStartOfYear now->当年开始时间 2021-01-01 00:00:00
func GetStartOfYear(t time.Time) time.Time {
	return time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, t.Location())
}

// GetEndOfYear 当年结束时间 2021-12-31 23:59:59
func GetEndOfYear(t time.Time) time.Time {
	return GetStartOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
}

// GetStartOfWeek now->当周开始时间 (周一算开始，周天结束) 2021-12-13 00:00:00
func GetStartOfWeek(t time.Time) time.Time {
	st := GetStartOfDay(t)
	weekDay := int(st.Weekday())
	if weekDay == int(time.Sunday) {
		return st.AddDate(0, 0, -6)
	}
	return st.AddDate(0, 0, -weekDay+1)
}

// GetEndOfWeek 当周结束时间 2021-12-19 23:59:59
func GetEndOfWeek(i time.Time) time.Time {
	return GetStartOfWeek(i).AddDate(0, 0, 7).Add(-time.Nanosecond)
}

// GetStartOfWeekSun now->当周开始时间 (周天算开始，周六结束) 2021-12-12 00:00:00
func GetStartOfWeekSun(t time.Time) time.Time {
	st := GetStartOfDay(t)
	weekDay := int(st.Weekday())
	return st.AddDate(0, 0, -weekDay)
}

// GetEndOfWeekSun now->当周开始时间 (周天算开始，周六结束) 2021-12-18 23:59:59
func GetEndOfWeekSun(t time.Time) time.Time {
	return GetStartOfWeekSun(t).AddDate(0, 0, 7).Add(-time.Nanosecond)
}

func Format(t time.Time, f string) string {
	return t.Format(f)
}
